//
//  KKStudyVC.m
//  DoMi
//
//  Created by 康凯 on 2016/12/14.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKStudyVC.h"
#import "UIColor+Hex.h"
#import "KKStudyHttp.h"
#import "KKStudyItem.h"
#import "KKItemListController.h"
#import "KKVideoDetailController.h"
#import "KKRankController.h"
#import "KKAllModuleDialog.h"
#import "KKSearchVC.h"
#import "SDCycleScrollView.h"
#import "KKVideo1Cell.h"
#import "KKBannerM.h"
#import "KKVideoDetailController.h"
#import "KKWebViewVC.h"
#import "KKStudyTopCell.h"
#import "KKTopItemCell.h"

@interface KKStudyVC ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(strong,nonatomic)NSMutableArray *bannerImgs;
@property(strong,nonatomic)NSArray *bannerDatas;
@property(weak,nonatomic)SDCycleScrollView *banner;
@property(strong,nonatomic)NSArray *itemDatas;
@property(strong,nonatomic)NSArray *homeList;
@end

@implementation KKStudyVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化nav
    [self initNav];
    //初始化view
    [self initView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getHomeList];
        [self getStudyItem];
        [self getBannerInfo];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark ---初始化nav
-(void)initNav{
    UIImageView *topTitle=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelg"]];
    self.navigationItem.titleView=topTitle;
    //左侧排行榜
    UIButton *leftBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [leftBtn setImage:[UIImage imageNamed:@"rank"] forState:UIControlStateNormal];
    [leftBtn setTitle:@"排行榜" forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(rank) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    //右侧搜索
    UIButton *rightBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [rightBtn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    
    [rightBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

#pragma mark ---初始化view
-(void)initView{
    //tableview相关设置
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKTopItemCell" bundle:nil] forCellReuseIdentifier:@"KKTopItemCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKStudyTopCell" bundle:nil] forCellReuseIdentifier:@"KKStudyTopCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKVideo1Cell" bundle:nil] forCellReuseIdentifier:@"KKVideo1Cell"];
}

#pragma mark ---tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        KKTopItemCell *top=(KKTopItemCell *)cell;
        top.studyVC=self;
        [top initData:self.itemDatas];
    }else if(indexPath.section==1){
        KKStudyTopCell *banner=(KKStudyTopCell *)cell;
        [banner initBanner:self.bannerDatas];
    }else{
        KKVideo1Cell *video=(KKVideo1Cell *)cell;
        [video initData:self.homeList[indexPath.row]];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==2){
        return self.homeList.count;
    }else{
        return 1;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        return [tableView dequeueReusableCellWithIdentifier:@"KKTopItemCell"];
    }else if(indexPath.section==1){
        return [tableView dequeueReusableCellWithIdentifier:@"KKStudyTopCell"];
    }else{
        return [tableView dequeueReusableCellWithIdentifier:@"KKVideo1Cell"];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==2){
        KKVideoDetailController *detail=[[KKVideoDetailController alloc]init];
        KKStudyList *list= self.homeList[indexPath.row];
        detail.videoId=list.videoid;
        [self.navigationController pushViewController:detail animated:YES];
    }
}


#pragma mark 排行榜按钮
-(void)rank{
    KKRankController *rank=[[KKRankController alloc] init];
    [self.navigationController pushViewController:rank animated:YES];
}
#pragma mark 搜索按钮
-(void)search{
    KKSearchVC *search=[[KKSearchVC alloc] initWithNibName:@"KKSearchVC" bundle:nil];
    [self.navigationController pushViewController:search animated:YES];
}


#pragma mark 接口--获取头部item
-(void)getStudyItem{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN};
    [KKStudyHttp getTopItem:params success:^(id datas) {
        self.itemDatas=datas;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
    }];
}

#pragma mark 接口--获取首页视频列表
-(void)getHomeList{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN};
    [KKStudyHttp getHomeList:params success:^(id datas) {
        self.homeList=datas;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
#pragma mark 接口--获取banner信息
-(void)getBannerInfo{
    [KKStudyHttp getBannerInfo:^(id datas) {
        self.bannerDatas=datas;
        KKLog(@"111--->%ld",self.bannerDatas.count);
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    } failure:^(NSError *error) {
        
    }];
}

@end
